package com.xjnt.tvos.tvlibs.jsbridge.executor;

import com.xjnt.tvos.tvlibs.jsbridge.data.DataBuilder;
import com.xjnt.tvos.tvlibs.jsbridge.data.ParamManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by caoyong on 18-1-8.
 */

public class NativeExecutor {
    private Object instance;
    private Method method;
    private ParamManager paramManager;

    private NativeExecutor(Object instance, Method method, ParamManager paramManager) {
        this.instance = instance;
        this.method = method;
        this.paramManager = paramManager;
    }

    public static NativeExecutor create(Object instance, Method method) {
        ParamManager paramManager = ParamManager.create(method);
        return new NativeExecutor(instance, method, paramManager);
    }

    public void invoke(DataBuilder data) {
        Object[] values = paramManager.getValues(data);
        try {
            this.method.invoke(instance, values);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public ParamManager getParamManager() {
        return paramManager;
    }
}
